package com.thti.mecool.client.model.biz;

import java.util.List;
import java.util.Map;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import com.thti.mecool.client.base.HandlerConstant;
import com.thti.mecool.client.db.DataBase;
import com.thti.mecool.client.model.po.TaskManage;

public class TaskManageBizImpl {
	private DataBase dataBase;
private Context context;
	public TaskManageBizImpl(Context context) {
		this.context=context;
		dataBase = new DataBase(context);
	}

	public void save(TaskManage taskManage) {
		SharedPreferences load = context.getSharedPreferences("LocationService", 0);
		String sqlString = "insert into taskManage(planId,projectId,taskId,stepId,data,isType,images,latitude,longitude,createTime,isUpload) values(?,?,?,?,?,?,?,?,?,?,0)";
		String latitude=load.getString("Latitude", null);
		String longitude=load.getString("Longitude", null);
		String localChangeTime=load.getString("LocalChangeTime", null);
		dataBase.execObject(
				sqlString, 
				new Object[] { taskManage.getPlanId(),
						taskManage.getProjectId(), taskManage.getTaskId(),
						taskManage.getStepId(), taskManage.getData(),
						taskManage.getIsType(), taskManage.getImages(),latitude,longitude,localChangeTime });
		dataBase.close(); 
	}
   
	@SuppressWarnings("unchecked") 
	public List<TaskManage> findByImage() {
		String sql = "select id,planId,projectId,taskId,stepId,images from taskStep where actionId=1 and isUpload=0";
		List<TaskManage> list =dataBase.findAll(TaskManage.class, sql, new String[]{});
		return list;
	}

	public void updateByImage(List<Map<String,Object[]>> list) {
		dataBase.getWritableDatabase().beginTransaction();//开始事务
		if(list!=null && list.size()>0){
			try {
				for (int i = 0; i < list.size(); i++) {
				   dataBase.getWritableDatabase().execSQL(HandlerConstant.UPDATE_TASKMANAGE_SQL,list.get(i).get(HandlerConstant.UPDATE_TASKMANAGE_SQL));
				}
				dataBase.getWritableDatabase().setTransactionSuccessful();//调用此方法会在执行到endTransaction() 时提交当前事务，如果不调用此方法会回滚事务
			} finally {
				dataBase.getWritableDatabase().endTransaction();//由事务的标志决定是提交事务，还是回滚事务
			} 
		}
		dataBase.getWritableDatabase().close(); 
	}

	public void deleteByStep(String planId, String projectId, String taskId,
			String stepId) {
		String sql = "delete from taskManage where planId=? and projectId=? and taskId=? and stepId=?";
		dataBase.execObject(sql, new Object[] { planId, projectId, taskId,
				stepId });
		dataBase.close();
	}

	public boolean findByStep(String planId, String projectId, String taskId,
			String stepId) {
		String sqlString = "delete from taskManage where planId=? and projectId=? and taskId=? and stepId=?";
		Cursor cursor = dataBase.queryObject(sqlString, new String[] { planId,
				projectId, taskId, stepId });

		if (cursor.moveToNext()) {
			boolean flag = cursor.getLong(0) > 0 ? true : false;
			cursor.close();
			dataBase.close();
			return flag;
		}
		cursor.close();
		dataBase.close();
		return false;

	}
	
	@SuppressWarnings("unchecked")
	public List<TaskManage> findAll() {
		String sql = "select planId,projectId,taskId,stepId,data,images,latitude,longitude,createTime from taskStep where isUpload<>2 and actionId in(1,2)";
		List<TaskManage> list =dataBase.findAll(TaskManage.class, sql, new String[]{});
		return list;

	}
}
